import React from "react";
import {
  withRouter
} from "react-router-dom";
import {
  Form,
  Input,
  Button,

} from 'antd';
import bossCss from './boss.module.css'

class Login extends React.Component {
  state = {
    loginApply: true,
    voucherId: '',
    applyId: ''

  }
  componentDidMount () {
    const query = this.props.location.search
    const arr = query.split('&')
    const voucherId = arr[0].slice((arr[0].indexOf("=") + 1))
    const applyId = arr[1].slice((arr[1].indexOf("=") + 1))
    this.getUserLoginInfo(voucherId, applyId)
    this.setState({})
  }

  //验证登录
  getUserLoginInfo (voucherId, applyId) {
    this.$axios({
      method: 'post',
      url: '/wx/portal/getLoginUser',
    }).then(res => {
      console.log(res, '登录信息')
      if (res.data.msg === "操作成功") {
        this.props.history.push({ pathname: "/bossDetail", state: { voucherId, applyId, userId: res.data.data.userId } })
      }
    }).catch(function (error) {
      console.log(error);
    })
  }

  submitApply = (data) => {
    const { voucherId, applyId } = this.state
    console.log(data, "登录信息")
    this.$axios({
      method: 'post',
      url: `/wx/portal/bossLogin?loginName=${data.loginName}&password=${data.password}`,
    }).then(res => {
      console.log(res, '数据')

      if (res.data === 'redirect:/login') {
        this.setState({ loginApply: false })

      } else if (res.data === 'redirect:/index') {
        this.getUserLoginInfo(voucherId, applyId)
      }

    }).catch(function (error) {
      console.log(error);
    })

  }

  render () {
    const loginApply = this.state.loginApply
    return (

      <div className={bossCss.login}>

        <Form
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 10 }}
          onFinish={this.submitApply}
        >

          <div style={{ fontSize: 20, fontWeight: "bold", textAlign: "center" }}>登录</div>
          {
            !loginApply ?
              <div style={{ color: "red" }}>登录失败,账号或密码错误,请重新登录</div> : ''
          }
          <Form.Item label="用户名" name="loginName" rules={[{ required: true, message: '请输入用户名' }]} >
            <Input />
          </Form.Item>
          <Form.Item label="密码" name="password" rules={[{ required: true, message: '请输入密码' }]} >
            <Input.Password placeholder="请输入密码" />
          </Form.Item>
          <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
            <Button type="primary" htmlType="submit" >
              提交
            </Button>
          </Form.Item>
        </Form>

      </div>

    );
  }
}



export default withRouter(Login)

